//
//  TemplateAddRWShowView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/18.
//

#import "TemplateAddRWShowView.h"
#import "TemplateFoundDetailsModel.h"

@interface TemplateAddRWShowView ()

@property (nonatomic ,strong) UIView * makeView;
@property (nonatomic ,strong) void(^rightblock)(NSArray<NSString *>* IDArray);
@property (nonatomic ,assign) CGFloat viewH;
@property (nonatomic ,strong) UIScrollView * scrollview;
@property (nonatomic ,strong) NSArray <TemplateFoundDetailsItemModelListModel *>* array;
@end
@implementation TemplateAddRWShowView

+ (instancetype)showviewWithId:(NSString *)ID andRightBlock:(void(^)(NSArray<NSString *>* IDArray))rightblock{

    TemplateAddRWShowView * view = [TemplateAddRWShowView new];
    [[DTKTool getCurrentVC].view addSubview:view];
    view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    view.viewH = kwt(400) + KSafeAreaBottomHeight;
    view.rightblock = rightblock;
    UIView * makeView = [UIView new];
    view.makeView = makeView;
    makeView.backgroundColor = UIColor.whiteColor;
    makeView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, kwt(170) + KSafeAreaBottomHeight);
    [view addSubview:makeView];
    [DTKTool BFYJ:kwt(15) andView:makeView andCorner:UIRectCornerTopLeft|UIRectCornerTopRight andSize: CGRectMake(0, 0 ,SCREEN_WIDTH, view.viewH + KSafeAreaBottomHeight)];
    
//    view.userInteractionEnabled = YES;
//    [view addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:view action:@selector(dismiss)]];
    makeView.userInteractionEnabled = YES;
    [makeView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:view action:@selector(noclick)]];
    view.userInteractionEnabled = YES;
    [view addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:view action:@selector(dismiss)]];
    makeView.userInteractionEnabled = YES;
    [makeView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:view action:@selector(noclick)]];
    
    UILabel * titlab = [DTKTool labelWithText:@"任务名称 (可多选)" andFont:kCFont(18) andTextColor:KcolorHex(@"#222222")];
    [makeView addSubview:titlab];
    [titlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.top.offset(kwt(21));
    }];
    
    UIButton * btn = [UIButton buttonWithType:0];
    [btn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    btn.addTarget(view,@selector(dismiss));
    [makeView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-15));
        make.width.height.offset(kwt(20));
        make.centerY.equalTo(titlab);
    }];
    
    
    
    UIView * buttonview = [UIView new];
    [makeView addSubview:buttonview];
    [buttonview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(78) + KSafeAreaBottomHeight);
    }];
    
    UIButton * qxbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qxbtn.backgroundColor = KcolorHex(@"#F0F2F4");
    [qxbtn setTitle:@"重置" forState:UIControlStateNormal];
    [qxbtn setTitleColor:KcolorHex(@"#5A6066") forState:UIControlStateNormal];
    qxbtn.titleLabel.font = kCFont(17);
    [DTKTool YJ:kwt(5) andView:qxbtn];
    [qxbtn addTarget:view action:@selector(leftBtnclick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:qxbtn];
    [qxbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(44));
        make.width.offset(kwt(160));
        make.left.offset(kwt(20));
        make.top.offset(kwt(17));
    }];
    
    UIButton * qrbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qrbtn.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(160), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#2892FE") endColor:KcolorHex(@"#0366FE")];
    [qrbtn setTitle:@"确定" forState:UIControlStateNormal];
    [qrbtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    qrbtn.titleLabel.font = kCFont(17);
    [DTKTool YJ:kwt(5) andView:qrbtn];
    [qrbtn addTarget:view action:@selector(rightbtnclick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:qrbtn];
    [qrbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(44));
        make.width.offset(kwt(160));
        make.right.offset(kwt(-20));
        make.top.offset(kwt(17));
    }];
    
    
    UIScrollView * scrollview = [UIScrollView new];
    [makeView addSubview:scrollview];
    view.scrollview = scrollview;
    [scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.right.offset(kwt(-15));
        make.top.offset(kwt(60));
        make.bottom.equalTo(buttonview.mas_top);
    }];
    
    
    
    [view show];
    [view PostTaskTypeWithID:ID];
    
    return  view;
    
    
}

- (void)PostTaskTypeWithID:(NSString *)ID{
    [self showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithDictionary:@{@"id":ID}];
    [SCNetManger PostMBTaskTypeWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self hideBusyHUD];
        if(model.code == 200){
            self.array = [NSArray yy_modelArrayWithClass:TemplateFoundDetailsItemModelListModel.class json:model.result];
            [self scrollviewSetUpview];
        } else {
            [self showWarning:model.message];
        }
    }];
}
- (void)scrollviewSetUpview{
    [self.scrollview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat X = 0;
    CGFloat Y = 0;
    MJWeakSelf
    for (TemplateFoundDetailsItemModelListModel * childModel in self.array) {//子选项
        UIView * btnview = [UIView new];
        btnview.addClickBlock(^{
            [weakSelf btnclick:childModel];
        });
        [DTKTool YJBK:kwt(6) andView:btnview andWidth:1 andColor:childModel.checkedFlag?KcolorHex(@"#0967FF"):KcolorHex(@"#BFBFBF")];
        UILabel * lab = [DTKTool labelWithText:childModel.name andFont:kFont(16) andTextColor:childModel.checkedFlag?KcolorHex(@"#0967FF"):KcolorHex(@"#373C45")];
        [btnview addSubview:lab];
        lab.textAlignment = NSTextAlignmentCenter;
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(kwt(10));
            make.right.offset(kwt(-10));
            make.centerY.offset(0);
        }];
        if(childModel.checkedFlag){
            UIImageView * xzimg = kimageView(@"template_selete");
            [btnview addSubview:xzimg];
            [xzimg mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.bottom.offset(0);
                make.height.offset(kwt(11));
                make.width.offset(kwt(10));
            }];
        }
        
        [self.scrollview addSubview:btnview];
        if(X + childModel.nameW > kwt(345)){
            X = 0;
            Y += kwt(48);
        }
        btnview.frame = CGRectMake(X, Y, childModel.nameW, kwt(32));
        X += childModel.nameW + kwt(14);
    }
    self.scrollview.contentSize = CGSizeMake(0, Y + kwt(48));
}

- (void)btnclick:(TemplateFoundDetailsItemModelListModel *)model{
    model.checkedFlag = (model.checkedFlag == 1)?0:1;
    [self scrollviewSetUpview];
}


- (void)leftBtnclick{
   
    for (TemplateFoundDetailsItemModelListModel * childModel in self.array) {//子选项
        childModel.checkedFlag = 0;
    }
    [self scrollviewSetUpview];
}

- (void)rightbtnclick{
    NSMutableArray * array = [NSMutableArray array];
    for (TemplateFoundDetailsItemModelListModel * childModel in self.array) {//子选项
        if(childModel.checkedFlag == 1){
            [array addObject:childModel.ID];
        }
    }
    if(self.rightblock && array.count > 0){
        self.rightblock(array);
    }
    [self dismiss];
}


- (void)show{
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT - self.viewH , SCREEN_WIDTH, self.viewH);
    }];
}
- (void)noclick{}
- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT , SCREEN_WIDTH, self.viewH);
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}



@end
